package org.ucas.faker.web.controller;


import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.beans.factory.annotation.Autowired;
//import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.ucas.faker.web.dao.patents.SymbolDao;
import org.ucas.faker.web.dto.PageDto;
import org.ucas.faker.web.dto.PatentDto;
import org.ucas.faker.web.entity.patents.Symbol;
import org.ucas.faker.web.entity.patents.Symbol.Category;
import org.ucas.faker.web.utils.feature.BranchAndEffect;
import org.ucas.faker.web.utils.model.RawPatent;




@Controller
@ResponseBody
public class TechAndEffController extends BaseController{


//  @RequestMapping(value = "/document/getTechEff",method = RequestMethod.GET)
//  public PageDto<BranchAndEffect> getTechEff(
//      @RequestParam(value = "searchQuery",required = false) String queryString,
//      @RequestParam(value = "pageNumber",required = false) String pageNumber,
//      @RequestParam(value = "perNumber",required = false) String perSize
//  ) throws URISyntaxException, IOException, InvalidFormatException, InterruptedException{
//	  System.out.println("get Tech Eff controller");
//    if(perSize==null||perSize.length()==0){
//      perSize = "20";
//    }
//    if(pageNumber==null||pageNumber.length()==0){
//        pageNumber = String.valueOf(1);
//      }else{
//      }
//    int pageN = Integer.parseInt(pageNumber);
//    
//    List<BranchAndEffect> list = mapper.getBranchAndEffect();
//    
////    for(BranchAndEffect l:list){
////    	System.out.println(l.getId()+"\n"+l.getAbstraction()+"\n"+l.getBranch()+"\n"+l.getEffect()+"\n");
////    }
//
//    List<PageDto<BranchAndEffect>> result = new ArrayList<>();//PatentDto.of(tmpResult);
//    
//    return PageDto.ofList(list, list.size(), pageN, Integer.parseInt(perSize));
//  }
	
@Autowired
SymbolDao symDao;

  @RequestMapping(value = "/bref/update",method = RequestMethod.GET)
  public String UpdateBrachEffect(
      @RequestParam(value = "id",required = false) String id,
      @RequestParam(value = "beforevalue",required = false) String beforevalue,
      @RequestParam(value = "currentvalue",required = false) String currentvalue
  ) throws URISyntaxException, IOException, InvalidFormatException, InterruptedException{
//	  System.out.println(id);
//	  System.out.println(beforevalue);
//	  System.out.println(currentvalue);
	  String res [] = id.split("_");
	  String patent_id = res[1];
	  List<Symbol> lsb = null;
	  Symbol sym = null;
	  if(res[0].equals("branch")){
		  lsb = symDao.findByPatentIdAndKindAndOriginal(Long.parseLong(patent_id), Category.BRANCH,beforevalue);

  }else if (res[0].equals("effect")){
	  lsb = symDao.findByPatentIdAndKindAndOriginal(Long.parseLong(patent_id), Category.EFFECT,beforevalue);
  }
	  if(lsb.size()>0){
		  sym = lsb.get(0);
	  }
	  if(sym!=null){
		  sym.setOriginal(currentvalue);
		  symDao.save(sym);
		  return "ok";
	  }
	  return "false";
  }
  
  @RequestMapping(value = "/bref/remove",method = RequestMethod.GET)
  public String removeBrachEffect(
      @RequestParam(value = "id",required = false) String id,
      @RequestParam(value = "currentvalue",required = false) String currentvalue
  ) throws URISyntaxException, IOException, InvalidFormatException, InterruptedException{
	  //System.out.println(id+"\t"+currentvalue);
	  String res [] = id.split("_");
	  String patent_id = res[1];
	  List<Symbol> lsb = null;
	  Symbol sym = null;
	  if(res[0].equals("branch")){
		  lsb = symDao.findByPatentIdAndKindAndOriginal(Long.parseLong(patent_id), Category.BRANCH,currentvalue);

  }else if (res[0].equals("effect")){
	  lsb = symDao.findByPatentIdAndKindAndOriginal(Long.parseLong(patent_id), Category.EFFECT,currentvalue);
  }
	  if(lsb.size()>0){
		  sym = lsb.get(0);
	  }
	  if(sym!=null){
		  symDao.delete(sym);
		  return "ok";
	  }
	  return "false";
  }
  
  @RequestMapping(value = "/bref/newValue",method = RequestMethod.GET)
  public String newBrachEffect(
      @RequestParam(value = "id",required = false) String id,
      @RequestParam(value = "currentvalue",required = false) String currentvalue
  ) throws URISyntaxException, IOException, InvalidFormatException, InterruptedException{
		//System.out.println(id + "\t" + currentvalue);
		//System.out.println(id + "\t" + currentvalue);
		String res[] = id.split("_");
		String patent_id = res[1];
		List<Symbol> lsb = null;
		Symbol sym = new Symbol();
		sym.setOriginal(currentvalue);
		sym.setPatentId(Long.parseLong(patent_id));
		if (res[0].equals("branch")) {
			sym.setKind(Category.BRANCH);
		} else if (res[0].equals("effect")) {
			sym.setKind(Category.EFFECT);
		}

		symDao.save(sym);
		return "ok";
  }
}

//(NOT simpleLegalState CONTAINS "有效") AND(simpleLegalState CONTAINS "有效")